package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPlayLinkBinding;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.TimeUtil;

public class PlayLinkActivity extends BaseAc<ActivityPlayLinkBinding> {

    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.pbPlay.setProgress(mDataBinding.videoView.getCurrentPosition() / 1000);
            mDataBinding.tvPlayLeft.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_play_link;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivLocalPlayBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivPlay.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivSearch) {//搜索
            String url = mDataBinding.etUrl.getText().toString();
            if (TextUtils.isEmpty(url)) {
                ToastUtils.showShort(getString(R.string.please_input_link));
                return;
            }
            setPlayer(url);
        } else if (id == R.id.ivPlay) {   //播放/暂停
            if (mDataBinding.videoView.isPlaying()) {
                mDataBinding.ivPlay.setImageResource(R.drawable.bf);
                mDataBinding.videoView.pause();
                stopTime();
            } else {
                mDataBinding.ivPlay.setImageResource(R.drawable.zanting);
                mDataBinding.videoView.start();
                startTime();
            }
        }
    }

    @Override
    protected void initData() {

    }

    private void setPlayer(String url) {
        mHandler = new Handler();
        showDialog(getString(R.string.loading));
        mDataBinding.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.url_error));
                return true;
            }
        });

        mDataBinding.videoView.setVideoURI(Uri.parse(url));
        mDataBinding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                dismissDialog();
                mDataBinding.llVideo.setVisibility(View.VISIBLE);
                mDataBinding.pbPlay.setMax((mDataBinding.videoView.getDuration() / 1000));
                mDataBinding.tvPlayRight.setText(TimeUtil.getMmss(mDataBinding.videoView.getDuration()));
                //直接播放视频
                mDataBinding.videoView.seekTo(1);
                mDataBinding.ivPlay.setImageResource(R.drawable.zanting);
                mDataBinding.videoView.start();
                startTime();
            }
        });
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.ivPlay.setImageResource(R.drawable.bf);
                mDataBinding.pbPlay.setProgress(0);
                mDataBinding.videoView.seekTo(1);
                mDataBinding.tvPlayLeft.setText("00:00");
                stopTime();
            }
        });
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        if (mHandler != null) {
            mHandler.removeCallbacks(mTaskUpdateTime);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlay.setImageResource(R.drawable.bf);
        mDataBinding.videoView.pause();
        stopTime();
    }

}